package com.gxa.insurance.controller;

import com.gxa.insurance.pojo.Card;
import com.gxa.insurance.service.CardService;
import com.gxa.insurance.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.util.JAXBSource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CardController
 * @Description TODO
 * @Author ds
 * @Date 2022/6/8 11:05
 * @Version 1.0
 */
@Api
@RestController
@RequestMapping("/card")
@CrossOrigin
public class CardController {

    @Autowired
    CardService cardService;


    @ApiOperation(value = "添加出主卡",produces = "body")
    @PostMapping("/add")
    public Map add(@RequestBody Card card) throws Exception {

        cardService.add(card);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "查询所有用户(分页)",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataType = "int"),
            @ApiImplicitParam(name = "size",value = "条数",dataType = "int",defaultValue = "10")
    })
    @PostMapping ("/getCards")
    public Map getCards(Integer page,Integer size) throws Exception {
        Map<String, Object> map = cardService.selectByPage(page, size);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("count",map.get("count"));
        jsonResult.setData(map.get("cards"));
        return jsonResult.getMap();
    }

    @ApiOperation("根据id查询(可配合删除使用)")
    @ApiImplicitParam(name = "id",value = "编号",paramType = "query")
    @PostMapping("/selectById")
    public Map selectById(@RequestParam("id") Integer id) throws Exception {
        List<Card> cards = cardService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(cards);
        return jsonResult.getMap();
    }

    @ApiOperation("修改车主卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "card",value = "车主卡对象",dataType = "Card",paramType = "body")
    })
    @PostMapping("/update")
    public Map update(@RequestBody Card card) throws Exception {
        cardService.update(card);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("更新成功");
        return jsonResult.getMap();
    }


    @ApiOperation("根据id删除用户")
    @ApiImplicitParam(name = "id",value = "用户编号",dataType = "int",paramType = "query")
    @PostMapping("/delete")
    public Map delete(@RequestParam("id") Integer id) throws Exception {
        cardService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据手机号或姓名模糊查询",produces = "body")
    @ApiImplicitParam(name = "(不传然和条件就是查询所有)查询条件字段  和分页数据  以json方式传入",value="传值示例，请保持字段名相同 " +
            "{\n" +
            "    \"name\":\"姓名或手机号\",\n" +
            "    \"page\":\"1\",\n" +
            "    \"size\":\"10\",\n" +
            "}\n")
    @PostMapping("/getLikeName")
    public Map getLikeName(@RequestBody Map<String,String> map){

        Map<String, Object> likeList = cardService.getLikeList(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(likeList.get("cards"));
        jsonResult.put("count",likeList.get("count"));

        return jsonResult.getMap();
    }


}
